<?php

namespace App\Models;

use Illuminate\Support\Collection;

class DiscountGoods extends Model
{
    const NORMAL = 1; // 活动正常
    const END    = 2; // 活动结束

    protected $table = 'yf_discount_goods';
    protected $primaryKey = 'discount_goods_id';
    public $timestamps = false;
    protected $guarded = ['discount_goods_id'];

    public function goodsBase()
    {
        return $this->hasOne(GoodsBase::class, 'goods_id', 'goods_id');
    }

    public static function getOpenForumDiscount(array $ids, int $size) : Collection
    {
        $columns = [
            'a.discount_goods_id', 'a.discount_id','a.discount_name', 'a.goods_id', 'a.common_id', 'b.goods_image',
            'a.goods_price', 'a.discount_price', 'a.goods_name', 'a.goods_end_time', 'a.goods_start_time', 'a.goods_lower_limit'
        ];

        return self::buildQuery($columns)
            ->when($ids, function ($query) use ($ids) {
                return $query->whereNotIn('a.discount_goods_id', $ids);
            })
            ->limit($size)
            ->get();
    }

    public static function getDiscountGoodsByIds(array $ids) : Collection
    {
        $now = now()->toDateTimeString();
        return self::where('goods_start_time', '<', $now)
            ->where('goods_end_time', '>', $now)
            ->where('discount_goods_state', 1)
            ->whereIn('discount_goods_id', $ids)
            ->get();
    }

    private static function buildQuery(array $columns)
    {
        $now = now()->toDateTimeString();
        return self::from('yf_discount_goods as a')
            ->select($columns)
            ->leftJoin('yf_goods_base as b', 'b.goods_id', '=', 'a.goods_id')
            ->leftJoin('yf_goods_common as c', 'c.common_id', '=', 'b.common_id')
            ->where([
                ['a.goods_start_time', '<', $now],
                ['a.goods_end_time', '>', $now],
                ['a.discount_goods_state', '=', 2],
                ['b.goods_is_shelves', '=', 1],
                ['b.is_del', '=', 1],
                ['c.common_state', '=', 1],
                ['c.common_verify', '=', 1],
                ['c.is_del', '=', 1]
            ])
            ->latest('a.discount_goods_id');
    }

    /**
     * 商品折扣详情
     * @param int $goodsId
     * @return array
     */
    public static function getNormalDiscountGoodsDetailByGoodsId(int $goodsId) : array
    {
        $data = [];
        $detail = self::where([
                ['goods_id', '=', $goodsId],
                ['discount_goods_state', '=', self::NORMAL]
            ])
            ->with('goodsBase')
            ->first();
        if ($detail && $detail->goodsBase) {
            // 活动结束
            if (now()->isAfter($detail->goods_end_time)) {
                $detail->discount_goods_state = self::END;
                $detail->save();
            }
            $detail->discount_percent = sprintf("%.1f", $detail->discount_price / $detail->goodsBase->goods_price * 10);
            $data = array_merge($detail->toArray(), $detail->goodsBase->only('goods_name', 'goods_price', 'goods_image'));
        }

        return $data;
    }
}
